package com.shushan.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.shushan.util.DensityUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @ClassName: RoundProgressBar
 * @Desciption: //圆环状进度条
 * @author: zhangshihao
 * @date: 2018-06-23
 */
public class RoundProgressBar extends View {

    private static final String TAG =  RoundProgressBar.class.getSimpleName();

    private int centerX;//圆环的中心X坐标
    private int centerY;//圆环的中心Y坐标
    private int radius;//圆环的半径
    private Paint paint;//圆环的画笔
    private Paint progressPaint;//圆环中进度的画笔
    private int progress;//当前进度
    private int maxProgress = 100;//最大进度，默认为100

    private Timer mTimer;

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public RoundProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RoundProgressBar(Context context) {
        super(context);
        init();
    }

    /**创建RoundProgressView时通过该方法初始化*/
    private void init(){
        paint = new Paint();
        paint.setStrokeWidth((float) DensityUtil.dip2px(getContext(),2.5f));
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setColor(Color.parseColor("#5D5D5D"));//黑色的圆环

        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);
        progressPaint.setColor(Color.parseColor("#FF4D2E"));//蓝色的进度
        progressPaint.setStrokeWidth((float) DensityUtil.dip2px(getContext(),2.5f));
        progressPaint.setStyle(Style.STROKE);
        //progressPaint.setStrokeCap(Paint.Cap.ROUND);//让画笔画出的线条是圆的
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //计算圆环的圆心坐标
        centerX = getMeasuredWidth() / 2;
        centerY = getMeasuredHeight() / 2;
        //计算圆环的半径，这里取View的长宽中较小值的1/2
        radius = centerX > centerY ? centerY - 5 : centerX - 5;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画出灰色的圆环
        canvas.drawCircle(centerX, centerY, radius, paint);
        //构造圆环的外围矩形
        RectF rectF = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
        //计算显示的进度
        int progressAngle = (progress * 360) / maxProgress;
        //画出进度，注意这里的270表示从圆环的最顶部开始画，如果270替换为0，则是从圆环的右端开始画
        canvas.drawArc(rectF, 270, progressAngle, false, progressPaint);
    }

    public void countBack(final RoundProgressBar pb,int mills){
        if(mills == 0){
            mills = 3000;
        }
        pb.setProgress(0);
        int percent = mills/100;
        Log.d(TAG,"percent == "+percent);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int i = 1;
            @Override
            public void run() {
                if(i < 100) {
                    pb.setProgress(i++);
                }else{
                    pb.setProgress(0);
                    timer.cancel();
                }
            }
        },0,percent);
        mTimer = timer;
    }

    public void stopCountBack(RoundProgressBar pb){
        if(mTimer == null){
            Log.i(TAG,"mTimer == null");
            return;
        }
        pb.setProgress(0);
        mTimer.cancel();
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }

    public int getMaxProgress() {
        return maxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
    }
}
